package com.hs.biye.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.biye.constant.MessageConstant;
import com.hs.biye.excepction.BaseException;
import com.hs.biye.mapper.*;
import com.hs.biye.model.DTO.InhospitalRequest;
import com.hs.biye.model.VO.InhospitalVo;
import com.hs.biye.model.VO.RegistrationVo;
import com.hs.biye.model.entity.*;
import com.hs.biye.model.enums.GenderEnum;
import com.hs.biye.model.reslut.ResultPage;
import com.hs.biye.service.InhospitalService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author haoshuai
* @description 针对表【inhospital】的数据库操作Service实现
* @createDate 2024-02-18 12:53:14
*/
@Service
public class InhospitalServiceImpl extends ServiceImpl<InhospitalMapper, Inhospital>
    implements InhospitalService{


    @Resource
    private InhospitalMapper inhospitalMapper;

    @Resource
    private RegistrationMapper registrationMapper;

    @Resource
    private SeedoctorMapper seedoctorMapper;

    @Resource
    private BedMapper bedMapper;

    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public ResultPage<InhospitalVo> getPageInhospital(InhospitalRequest inhospitalRequest) {
        Integer page = inhospitalRequest.getPages();
        Integer size = inhospitalRequest.getSize();

        IPage<InhospitalVo> page2 = new Page<>(page, size);
        inhospitalMapper.selectMyPage(page2,inhospitalRequest);

        List<InhospitalVo> records = page2.getRecords();
        Long total = page2.getTotal();
        Long current = page2.getCurrent();
        Long pages = page2.getPages();
        records.forEach(it->{
            String gender = it.getGender();
            Integer integer = Integer.valueOf(gender);
            String valueByCode = GenderEnum.getValueByCode(integer);
            it.setGender(valueByCode);
        });
        return ResultPage.success(records,total,current,pages);
    }


    //根据id查询
    @Override
    public InhospitalVo getPageInhospitalById(Integer id) {
        InhospitalVo inhospitalVo = inhospitalMapper.selectbyid(id);
        return inhospitalVo;
    }

    @Override
    public boolean removeInhospital(Integer id) {
        if (id == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        return removeById(id);
    }


    //添加住院信息
    @Override
    @Transactional
    public boolean addInhospital(InhospitalRequest inhospitalRequest) {
        if (inhospitalRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        //将该挂号设置为已处理
        Registration registration = new Registration();
        registration.setRegistrationId(inhospitalRequest.getRegistrationId());
        registration.setIsDeal(1);
        int i = registrationMapper.updateById(registration);

        //设置病床的状态
        Bed bed = new Bed();
        bed.setBedId(inhospitalRequest.getBedId());
        bed.setDoctorId(inhospitalRequest.getDoctorId());
        bed.setPatientId(inhospitalRequest.getPatientId());
        bed.setStatue(1);
        int i1 = bedMapper.updateById(bed);
        if (i1 == 0){
            throw new BaseException(MessageConstant.SYSTEM_EOOR);
        }

        //添加住院信息
        Inhospital inhospital = new Inhospital();
        BeanUtils.copyProperties(inhospitalRequest,inhospital);
        Employee employee = employeeMapper.selectById(inhospitalRequest.getDoctorId());
        inhospital.setDepartmentId(employee.getDepartmentId());

        //添加就诊信息
        Seedoctor seedoctor = new Seedoctor();
        seedoctor.setPatientId(inhospitalRequest.getPatientId());
        seedoctor.setEmployeeId(inhospitalRequest.getDoctorId());

        Description description = new Description();
        description.setMsg("办理住院");
        description.setList(null);
        description.setIfInHospital("是");
        String jsonStr = JSONUtil.toJsonStr(description);
        seedoctor.setDescription(jsonStr);
        seedoctorMapper.insert(seedoctor);
        return save(inhospital);
    }


    //更新
    @Override
    @Transactional
    public boolean updateInhospital(InhospitalRequest inhospitalRequest) {
        if (inhospitalRequest == null) {
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }
        Integer bedId = inhospitalRequest.getBedId();
        Integer oldBedId = inhospitalRequest.getOldBedId();
        Integer patientId = inhospitalRequest.getPatientId();
        Integer doctorId = inhospitalRequest.getDoctorId();
        //修改病床id
        if (bedId != oldBedId){
            //修改病床的状态
            Bed oldbed = new Bed();
            oldbed.setBedId(oldBedId);
            oldbed.setPatientId(-1);
            oldbed.setDoctorId(-1);
            oldbed.setStatue(0);
            int i = bedMapper.updateById(oldbed);

            Bed bed = new Bed();
            bed.setBedId(bedId);
            bed.setPatientId(patientId);
            bed.setDoctorId(doctorId);
            bed.setStatue(1);
            int i2 = bedMapper.updateById(bed);
            if (i==0 || i2 == 0){
                throw new BaseException(MessageConstant.SYSTEM_EOOR);
            }
        }

        //更改住院信息
        Inhospital inhospital = new Inhospital();
        BeanUtils.copyProperties(inhospitalRequest,inhospital);
        boolean res = updateById(inhospital);

        return res;
    }


    //出院
    @Override
    public boolean OutHospital(Integer id) {
        if (id == null){
            throw new BaseException(MessageConstant.PAR_NOT_RIGHT);
        }


        //设置出院日期。
        Inhospital inhospital = getById(id);
        inhospital.setOutTime(LocalDate.now());

        //设置病床的状态
        Bed bed = new Bed();
        bed.setBedId(inhospital.getBedId());
        bed.setDoctorId(-1);
        bed.setPatientId(-1);
        bed.setStatue(0);
        bedMapper.updateById(bed);


        boolean res = updateById(inhospital);
        if (!res){
            throw new BaseException(MessageConstant.SYSTEM_EOOR);
        }
        return res;
    }
}




